package com.liang.lingdong.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liang.lingdong.common.BaseResponse;
import com.liang.lingdong.common.ErrorCode;
import com.liang.lingdong.common.ResultUtils;
import com.liang.lingdong.exception.BusinessException;
import com.liang.lingdong.model.topic.Topic;
import com.liang.lingdong.model.topic.dto.CreateTopicDto;
import com.liang.lingdong.model.topic.dto.QueryTopicListDto;
import com.liang.lingdong.model.topic.dto.UpdateTopicDto;
import com.liang.lingdong.model.topic.vo.TopicVo;
import com.liang.lingdong.service.TopicService;
import org.ehcache.xml.model.ThreadPoolsType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 题目
 */

@RestController
@RequestMapping("/topic")
public class TopicController {

    @Resource
    private TopicService topicService;

    /**
     * 创建题目
     * @param createTopicDto 参数
     * @return 结果
     */
    @PostMapping("/create")
    public BaseResponse<Long> createTopic(@RequestBody @Validated CreateTopicDto createTopicDto) {
        if (createTopicDto == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long topicId = topicService.createTopic(createTopicDto);
        return ResultUtils.success(topicId);
    }

    /**
     * 更新题目
     */
    @PutMapping("/update")
    public BaseResponse<Boolean> updateTopic(@RequestBody @Validated UpdateTopicDto updateTopicDto) {
        if (updateTopicDto == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = topicService.updateExam(updateTopicDto);
        return ResultUtils.success(result);
    }

    /**
     * 删除题目
     */
    @DeleteMapping("/delete")
    public BaseResponse<Boolean> deleteTopicById(Long topicId) {
        if (topicId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = topicService.deleteTopicById(topicId);
        return ResultUtils.success(result);
    }


    /**
     * 根据id查询题目
     */
    @GetMapping("/get")
    public BaseResponse<TopicVo> getTopicById(Long topicId) {
        if (topicId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Topic topic = topicService.getById(topicId);
        return ResultUtils.success(BeanUtil.toBean(topic, TopicVo.class));
    }

    /**
     * 根据id查询题目
     */
    @GetMapping("/get/admin")
    public BaseResponse<Topic> getTopicByIdIsAdmin(Long topicId) {
        if (topicId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Topic topic = topicService.getById(topicId);
        return ResultUtils.success(topic);
    }


    /**
     * 查询题目列表
     */
    @GetMapping("/list")
    public BaseResponse<List<TopicVo>> getTopicList(String topicName) {
        List<Topic> topicList = topicService
                .lambdaQuery()
                .like(Topic::getTopicName, topicName)
                .list();
        return ResultUtils.success(BeanUtil.copyToList(topicList, TopicVo.class));
    }

    /**
     * 根据考试id查询题目
     */
    @GetMapping("/list/exam")
    public BaseResponse<List<TopicVo>> getTopicListByExamId(Long examId) {
        if (examId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<TopicVo> list = topicService.getTopicListByExamId(examId);
        return ResultUtils.success(list);
    }

    /**
     * 解绑题目
     */
    @DeleteMapping("/unbind")
    public BaseResponse<Boolean> unbindTopic(Long topicId) {
        if (topicId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = topicService.unbindTopic(topicId);
        return ResultUtils.success(result);
    }

    /**
     * 分页查询题目
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<TopicVo>> getTopicListByPage(QueryTopicListDto queryTopicListDto) {
        if (queryTopicListDto == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<TopicVo> topicVoIPage = topicService.queryTopicListByPage(queryTopicListDto);
        return ResultUtils.success(topicVoIPage);
    }
}
